<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

namespace Admin\Controller;

use Think\Page;
use Think\Verify;

class AdminController extends BaseController {
    /**
     * 后台首页
     */
    public function index() {
        $res = $list = array();
        $keywords = I('keywords');
        if (empty($keywords)) {
            $res = D('admin')->select();
        } else {
            $res = D()->query("select * from __PREFIX__admin where user_name like '%$keywords%' order by admin_id");
        }
        $roleRs = D('admin_role')->getField('role_id,role_name');
        if ($res && $roleRs) {
            foreach ($res as $val) {
                $val['role'] = $roleRs[$val['role_id']];
                $val['add_time'] = date('Y-m-d H:i:s', $val['add_time']);
                $list[] = $val;
            }
        }
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 后台信息
     */
    public function adminInfo() {
        $adminId = I('get.admin_id', 0);
        if ($adminId) {
            $info = D('admin')->where("admin_id=$adminId")->find();
            $info['password'] = "";
            $this->assign('info', $info);
        }
        $act = empty($adminId) ? 'add' : 'edit';
        $roleRs = D('admin_role')->where('1=1')->select();
        $this->assign('act', $act);
        $this->assign('role', $roleRs);
        $this->display();
    }

    /**
     * 后台操作
     */
    public function adminHandle() {
        $post = I('post.');
        if (empty($post['password'])) {
            unset($post['password']);
        } else {
            $post['password'] = encrypt($post['password']);
        }
        if ($post['act'] == 'add') {
            unset($post['admin_id']);
            $post['add_time'] = time();
            if (D('admin')->where("user_name='" . $post['user_name'] . "'")->count()) {
                $this->error("此用户名已被注册，请更换", U('Admin/Admin/adminInfo'));
            } else {
                $rs = D('admin')->add($post);
            }
        }

        if ($post['act'] == 'edit') {
            $rs = D('admin')->where('admin_id=' . $post['admin_id'])->save($post);
        }

        if ($post['act'] == 'del' && $post['admin_id'] > 1) {
            $rs = D('admin')->where('admin_id=' . $post['admin_id'])->delete();
            exit(json_encode(1));
        }

        if ($rs) {
            $this->success("操作成功"); //',$jumpUrl='',$ajax=false) {
        } else {
            $this->error("操作失败");
        }
    }

    /**
     * 退出登陆
     */
    public function logout() {
        session_unset();
        session_destroy();
        $this->redirect('/Admin');
    }

    /**
     * 验证码获取
     */
    public function vertify() {
        $config = array(
            'fontSize' => 30,
            'length' => 4,
            'useCurve' => true,
            'useNoise' => false,
            'reset' => false
        );
        $Verify = new Verify($config);
        $Verify->entry("admin_login");
    }

    /**
     * 角色页面
     */
    public function role() {
        $rs = D('admin_role')->order('role_id desc')->select();
        $this->assign('list', $rs);
        $this->display();
    }

    /**
     * 角色信息
     */
    public function roleInfo() {
        $roleId = I('get.role_id');
        $tree = $detail = array();
        if ($roleId) {
            $detail = M('admin_role')->where("role_id=$roleId")->find();
            $detail['act_list'] = explode(',', $detail['act_list']);
            $this->assign('detail', $detail);
        }
        $menuRs = M('sys_menu')->order('id')->select();
        foreach ($menuRs as $val) {
            if (!empty($detail)) {
                $val['enable'] = in_array($val['id'], $detail['act_list']);
            }
            $modules[$val['group']][] = $val;
        }
        //权限组
        $group = array('system' => '系统设置', 'content' => '内容管理', 'goods' => '商品中心', 'member' => '会员中心',
            'order' => '订单中心', 'marketing' => '营销推广', 'tools' => '插件工具', 'count' => '统计报表', 'application' => '应用管理', 'distribut' => '分销管理'
        );
        $this->assign('group', $group);
        $this->assign('modules', $modules);
        $this->display();
    }

    /**
     * 保存角色
     */
    public function roleSave() {
        $post = I('post.');
        $res = $post['data'];
        $res['act_list'] = is_array($post['right']) ? implode(',', $post['right']) : '';
        if (empty($post['role_id'])) {
            $rs = D('admin_role')->add($res);
        } else {
            $rs = D('admin_role')->where('role_id=' . $post['role_id'])->save($res);
        }
        if ($rs) {
            adminLog('管理角色', __ACTION__);
            $this->success("操作成功!", U('Admin/Admin/roleInfo', array('role_id' => $post['role_id'])));
        } else {
            $this->success("操作失败!", U('Admin/Admin/role'));
        }
    }

    /**
     * 删除角色
     */
    public function roleDel() {
        $roleId = I('post.role_id');
        $adminInfo = D('admin')->where('role_id=' . $roleId)->find();
        if ($adminInfo) {
            exit(json_encode("请先清空所属该角色的管理员"));
        } else {
            $d = M('admin_role')->where("role_id=$roleId")->delete();
            if ($d) {
                exit(json_encode(1));
            } else {
                exit(json_encode("删除失败"));
            }
        }
    }

    /**
     * 日志
     */
    public function log() {
        $adminLogMod = M('admin_log');
        $p = I('p', 1);
        $logRs = $adminLogMod->join('__ADMIN__ ON __ADMIN__.admin_id =__ADMIN_LOG__.admin_id')->order('log_time DESC')->page($p . ',20')->select();
        $this->assign('list', $logRs);
        $count = $adminLogMod->where('1=1')->count();
        $Page = new \Think\Page($count, 20);
        $show = $Page->show();
        $this->assign('page', $show);
        $this->display();
    }

    /**
     * 供应商列表
     */
    public function supplier() {
        $supplierMod = M('');
        $prefix = C('DB_PREFIX');
        $count = $supplierMod->table($prefix . 'supplier')->where('')->count();
        $page = new Page($count, 10);
        $show = $page->show();
        $supplierRs = $supplierMod->field('s.*,a.admin_id,a.user_name')->table($prefix . 'supplier s')
                ->join('LEFT JOIN ' . $prefix . 'admin AS a ON a.supplier_id = s.supplier_id')
                ->limit($page->firstRow, $page->listRows)->select();
        $this->assign('list', $supplierRs);
        $this->assign('page', $show);
        $this->display();
    }

    /**
     * 供应商资料
     */
    public function supplierInfo() {
        $supplierId = I('get.supplier_id', 0);
        if ($supplierId) {
            $prefix = C('DB_PREFIX');
            $supplierMod = M('supplier');
            $supplierInfo = $supplierMod->field('s.*,a.admin_id,a.user_name')->table($prefix . 'supplier s')
                    ->join('LEFT JOIN ' . $prefix . 'admin AS a ON a.supplier_id = s.supplier_id')
                    ->where(array('s.supplier_id' => $supplierId))->find();
            $this->assign('info', $supplierInfo);
        }
        $act = empty($supplierId) ? 'add' : 'edit';
        $this->assign('act', $act);
        $adminRs = M('admin')->field('admin_id,user_name')->where('1=1')->select();
        $this->assign('admin', $adminRs);
        $this->display();
    }

    /**
     * 供应商增删改
     */
    public function supplierHandle() {
        $post = I('post.');
        $supplierMod = M('supplier');
        $adminMod = M('admin');
        if ($post['act'] == 'add') {
            unset($post['supplier_id']);
            $count = $supplierMod->where("supplier_name='" . $post['supplier_name'] . "'")->count();
            if ($count) {
                $this->error("此供应商名称已被注册，请更换", U('Admin/Admin/supplierInfo'));
            } else {
                $rs = $supplierMod->add($post);
                if (!empty($post['admin_id'])) {
                    $adminData['supplier_id'] = $supplierMod->getLastInsID();
                    $adminMod->where(array('supplier_id' => $adminData['supplier_id']))->save(array('supplier_id' => 0));
                    $adminMod->where(array('admin_id' => $post['admin_id']))->save($adminData);
                }
            }
        }
        if ($post['act'] == 'edit' && $post['supplier_id'] > 0) {
            $rs = $supplierMod->where('supplier_id=' . $post['supplier_id'])->save($post);
            if (!empty($post['admin_id'])) {
                $adminData['supplier_id'] = $post['supplier_id'];
                $adminMod->where(array('supplier_id' => $adminData['supplier_id']))->save(array('supplier_id' => 0));
                $adminMod->where(array('admin_id' => $post['admin_id']))->save($adminData);
            }
        }
        if ($post['act'] == 'del' && $post['supplier_id'] > 0) {
            $rs = $supplierMod->where('supplier_id=' . $post['supplier_id'])->delete();
            $adminMod->where(array('supplier_id' => $post['supplier_id']))->save(array('supplier_id' => 0));
        }

        if ($rs !== false) {
            $this->success("操作成功", U('Admin/Admin/supplier'));
        } else {
            $this->error("操作失败", U('Admin/Admin/supplier'));
        }
    }

}
